package com.lin.videospringboot.controller;

import com.lin.videospringboot.Utils.HttpSender;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class Connector {

    @Value("${server.port}")
    public void setPport(String pport) {
        Connector.pport = pport;
    }

    @Value("${connector.ip}")
    public void setIp(String ip) {
        Connector.ip = ip;
    }

    @Value("${connector.port}")
    public void setPort(String port) {
        Connector.port = port;
    }



    @Value("${connector.url}")
    public void setUrl(String url) {
        Connector.url = url;
    }

    @Value("${connector.healthURL}")
    public void setHealthURL(String healthURL) {
        Connector.healthURL = healthURL;
    }

    @Value("${connector.pip}")
    public void setPip(String pip) {
        Connector.pip = pip;
    }
    static private String url;

    static private String healthURL;

    static private String ip;

    static private String port;

    static private String pip;

    static private String pport;

    static private boolean isConnect = false;
    static public void connect() {
        String urlName = "http://"+ip+":"+port;
        String param = "ip="+pip+"&url=" + url + "&"+"port="+pport+"&healthURL=" + healthURL;
        String res;
        try {
            res = HttpSender.sendPost(urlName,param);
            if(!isConnect) {
                System.out.println(res);
                isConnect = true;
            }
        } catch (Exception e) {
            if(isConnect) {
                System.out.println("与服务器断开连接");
            }
            isConnect = false;
        }
    }
}
